﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.Collections.Generic;

namespace DBtuning
{
    public partial class Config : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonReset_Click(object sender, EventArgs e)
        {
            saveConfig(true);
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            saveConfig(false);
        }

        void saveConfig(bool dflt)
        {
            XmlDocument configDoc = XmlDataSourceConfig.GetXmlDocument();
            XmlNodeList configList = configDoc.GetElementsByTagName("PerfConfig");
            string paramName;
            string paramValue;
            string paramWarningValue;
            string paramDefault;
            string paramDefaultWarning;
            for (int i = 0; i < GridViewConfig.Rows.Count; i++) //each (GridViewRow row in GridViewConfig.Rows)
            {
                try
                {
                    XmlElement elem = (XmlElement)configList[i];
                    GridViewRow row = GridViewConfig.Rows[i];
                    paramName = row.Cells[0].Text;
                    paramDefault = row.Cells[5].Text;
                    paramDefaultWarning = row.Cells[6].Text;

                    if (dflt)
                    {
                       
                        try
                        {
                            elem.Attributes["Threshold"].Value = paramDefault;
                            elem.Attributes["WarningValue"].Value = paramDefaultWarning;
                        }
                        catch { }
                    }
                    else
                    {
                        paramValue = ((TextBox)row.Cells[2].FindControl("ThresholdValue")).Text.Trim();
                        if (paramValue.Equals(String.Empty) || paramValue.Length == 0)
                        {
                            elem.Attributes["Threshold"].Value = paramDefault;
                        }
                        else
                        {
                            elem.Attributes["Threshold"].Value = paramValue;
                        }

                        paramWarningValue = ((TextBox)row.Cells[3].FindControl("WarningValue")).Text.Trim();
                        if (!paramWarningValue.Equals(String.Empty) && paramWarningValue.Length > 0)
                        {
                            elem.Attributes["WarningValue"].Value = paramWarningValue;
                        }
                        else
                        {
                            elem.Attributes["WarningValue"].Value = paramDefaultWarning;
                        }

                    }
                    
                }
                catch (Exception ex) { Response.Write(ex + "<hr />"); }
            }
            configDoc.Save(Server.MapPath(XmlDataSourceConfig.DataFile));
            Response.Redirect(Request.Url.ToString());

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Here");
        }

    }
}
